package net.oschina.app.adapter;

import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by YL on 2017/4/2.
 */

public class TabVPAdpater extends FragmentStatePagerAdapter {
    List<Class<? extends Fragment>> pagers = new ArrayList<>();
    List<String> tabTitles = new ArrayList<>();
    List<Bundle> bundles = new ArrayList<>();
    Context context;

    public TabVPAdpater(Context context, FragmentManager fm) {
        super(fm);
        this.context = context;
    }

    //对外提供一个初始化数据的方法
    public void setPagers(List<Class<? extends Fragment>> pagers) {
        this.pagers = pagers;
        notifyDataSetChanged();
    }

    //对外提供一个初始化页签的方法
    public void addTab(String title, Class<? extends Fragment> clz, Bundle bundle) {
        pagers.add(clz);
        tabTitles.add(title);
        bundles.add(bundle);
        notifyDataSetChanged();
    }

    @Override
    public Fragment getItem(int position) {
//        Fragment fragment = Fragment.instantiate(context, pagers.get(position).getName(), bundles.get(position));
        Fragment fragment = Fragment.instantiate(context, pagers.get(position).getName());
        return fragment;
    }

    @Override
    public int getCount() {
        return pagers.size();
    }

    //TabLayout的页签的标题要依赖该方法的返回值
    @Override
    public CharSequence getPageTitle(int position) {
        return tabTitles.get(position);
    }

}
